package wili.exp.simple;

import wili.c.Ca;
import wili.logic.AndOrExp;

/**
 * 
 * @author weilai
 *
 */
public class Sexp_test {
	public static void basicTest(int opt) {
		if (opt < 0)
			return;
		Scont cont = new Scont().puts("a", 2, "b", 3, "c", 4, "d", 1);
		Object[][] tt = { { "true", true }, //
				{ "a+b>c", true }, //
				{ "a*b+c/d+10", 20 }, //
				{ "a+b-c-d", 0 }, //
				{ "a+b<c", false }, //
				{ "a+b==5", true }, //
				{ "a+b!=5", false }, //
				{ "a=b+5", 8 }, //
				{ "b-=10", -7 },//
		};
		Ca.log(1, "=======basicTest");
		for (int i = 0; i < tt.length; i++) {
			Object[] test = tt[i];
			String expSt = test[0].toString();
			Object expect = test[1];
			Sexp exp = Sexp.newInst(expSt);
			exp.toString();
			Ca.log(i + "-------exp: " + expSt);
			if (i == 7)
				Ca.pause();
			Object ret = exp.value(cont);
			if (expect.equals(ret)) {
			} else {
				Ca.asert(false, "expect:" + expect + ", not:" + ret);
			}
		}
	}

	public static void bracket_test(int opt) {
		if (opt < 0)
			return;
		Scont cont = new Scont().puts("a", 2, "b", 3, "c", 4, "d", 1, "z", 10);
		Object[][] tt = { { "z-a+b-c", 7 }, //
				{ "z-(a+b)-c", 1 }, //
				{ "z-(a+b-1)-c", 2 }, };
		Ca.log(1, "=======bracket_test");
		for (int i = 0; i < tt.length; i++) {
			Object[] test = tt[i];
			String expSt = test[0].toString();
			Object expect = test[1];
			Ca.log(i + "-------exp: " + expSt);
			Sexp exp = Sexp.newInst(expSt);
			exp.toString();
			Object ret = exp.value(cont);
			if (expect.equals(ret)) {
			} else {
				Ca.asert(false, "expect:" + expect + ", not:" + ret);
			}
		}
	}

	public static void AndOrExp_test(int opt) {
		if (opt < 0)
			return;
		Scont cont = new Scont().puts("a", 2, "b", 3, "c", 4, "d", 1);
		Object[][] tt = { { "a>b || b<c", true }, //
				{ "(a>b || b<c) && c%2==0", true }, };
		Ca.log(1, "=======AndOrExp_test");
		for (int i = 0; i < tt.length; i++) {
			Object[] test = tt[i];
			String expSt = test[0].toString();
			Object expect = test[1];
			AndOrExp exp = AndOrExp.newInst(1, expSt);
			exp.toString();
			Boolean ret = exp.isTrue(cont);
			Ca.log(i + "-------exp: " + expSt);
			if (expect.equals(ret)) {
			} else {
				Ca.asert(false, "expect:" + expect + ", not:" + ret);
			}
		}
	}

	public static void function_test(int opt) {
		if (opt < 0)
			return;
		Scont cont = new Scont().puts("a", 2, "b", 3, "c", 4, "d", 1, "z", 10);
		Object[][] tt = { { "fun(11,a)", 7 }, //
				{ "obj.test(b,c)", 1 } //
		};
		Ca.asert(false, "todo12-12");
		Ca.log(1, "=======function_test");
		for (int i = 0; i < tt.length; i++) {
			Object[] test = tt[i];
			String expSt = test[0].toString();
			Object expect = test[1];
			Ca.log(i + "-------exp: " + expSt);
			Sexp exp = Sexp.newInst(expSt);
			exp.toString();
			Object ret = exp.value(cont);
			if (expect.equals(ret)) {
			} else {
				Ca.asert(false, "expect:" + expect + ", not:" + ret);
			}
		}
	}

	public static void main(String[] args) {
		if ("456".isEmpty()) {
			function_test(1);
			return;
		}
		bracket_test(1);
		basicTest(1);
		AndOrExp_test(1);
		function_test(-1);
	}
}
